package model

import (
	"global/libs/structs"
	"github.com/shopspring/decimal"
	"time"
)

type coinTickerModel struct {}

var CoinTickerModel = new(coinTickerModel)

func (m *coinTickerModel) nullToZero(value string) string {
	v, _ := decimal.NewFromString(value)
	return v.StringFixedBank(8)
}

// 插入数据
func (m *coinTickerModel) InsertCoinTicker(mid int, t *structs.CoinTicker) (int, error) {
	ticker := &CoinTicker{
		Mid: 				mid,
		Symbol: 			t.Symbol,
		Volume: 			m.nullToZero(t.Volume),
		Amount: 			m.nullToZero(t.Volume),
		Open: 				m.nullToZero(t.Open),
		High: 				m.nullToZero(t.High),
		Low: 				m.nullToZero(t.Low),
		Close: 				m.nullToZero(t.Close),
		Percent: 			m.nullToZero(t.Percent),
		TotoalSupply: 		m.nullToZero(t.TotalSupply),
		CirculatingSupply: 	m.nullToZero(t.CirculatingSupply),
		MarketValue: 		m.nullToZero(t.MarketValue),
		TurnoverRate: 		m.nullToZero(t.TurnoverRate),
		NumMarketPairs: 	int(t.NumMarketPairs),
		NumExchange: 		int(t.NumExchange),
		Usd: 				m.nullToZero(t.USD),
		Cny: 				"0.0000",
		LastUpdated: 		time.Unix(t.LastUpdated, 0),
	}
	_, err := MainDB.Table("coin_ticker").Insert(ticker)
	if err != nil {
		return 0, err
	}
	return ticker.Id, err
}